package tw.asap.plus.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;
import tw.asap.plus.entity.response.Address;
import tw.asap.plus.ui.activity.AddressEditActivity;
import tw.asap.plus.ui.activity.AddressListActivity;

/**
 * Created by Yax.
 * Date: 15/11/14
 * Time: 下午3:46
 * Description:
 */
public class AddressAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {

    Context context;
    List<Address> addressList;
    String selectedId = "";

    public AddressAdapter(Context context, List<Address> addressList) {
        this.context = context;
        this.addressList = addressList;
    }

    public AddressAdapter(Context context, List<Address> addressList, String selectedId) {
        this.context = context;
        this.addressList = addressList;
        this.selectedId = selectedId;
    }


    @Override
    public int getCount() {
        return addressList != null ? addressList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return addressList != null ? addressList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_address, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvName.setText(addressList.get(position).getName());
        holder.tvMobile.setText(addressList.get(position).getMobile());
        holder.tvAddress.setText(addressList.get(position).getArea().concat(addressList.get(position).getAddress()));
        holder.icDefault.setVisibility(addressList.get(position).getIs_default().equals("1") ? View.VISIBLE : View.GONE);
        if (!TextUtils.isEmpty(selectedId) && selectedId.equals(addressList.get(position).getAddrid())) {
            holder.icIndicator.setVisibility(View.VISIBLE);
        } else {
            holder.icIndicator.setVisibility(View.GONE);
        }
        holder.edit.setOnClickListener(v -> {
            Intent intent = new Intent(context, AddressEditActivity.class);
            intent.putExtra("ADDRESS", addressList.get(position));
            ((Activity) context).startActivityForResult(intent, AddressListActivity.ADDRESS_EDIT);
        });

        return convertView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if (((Activity) context).getIntent().getBooleanExtra("FROM_MINE", false)) {
            Intent intent = new Intent(context, AddressEditActivity.class);
            intent.putExtra("ADDRESS", addressList.get(position));
            ((Activity) context).startActivityForResult(intent, AddressListActivity.ADDRESS_EDIT);
        } else {
            if (!addressList.get(position).getAddrid().equals(selectedId)) {
                for (int i = parent.getFirstVisiblePosition(); i < parent.getLastVisiblePosition(); i++) {
                    if (addressList.get(i).getAddrid().equals(selectedId)) {
                        int childIndex = i - parent.getFirstVisiblePosition();
                        parent.getChildAt(childIndex).findViewById(R.id.ic_indicator).setVisibility(View.GONE);
                    }
                }
                this.selectedId = addressList.get(position).getAddrid();
                view.findViewById(R.id.ic_indicator).setVisibility(View.VISIBLE);
            }

            Intent intent = new Intent();
            intent.putExtra("ADDRESS", addressList.get(position));
            ((Activity) context).setResult(Activity.RESULT_OK, intent);
            ((Activity) context).finish();
        }
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_address.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @InjectView(R.id.tv_name)
        TextView tvName;
        @InjectView(R.id.tv_mobile)
        TextView tvMobile;
        @InjectView(R.id.ic_default)
        TextView icDefault;
        @InjectView(R.id.tv_address)
        TextView tvAddress;
        @InjectView(R.id.line_split)
        View lineSplit;
        @InjectView(R.id.edit)
        ImageView edit;
        @InjectView(R.id.ic_indicator)
        View icIndicator;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
